/*************************************************************************** 
 *   Copyright (C) 2007 by Sassan Panahinejad                              *
 *   ico2ico2@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
 
#include <stdarg.h>
#include <libgen.h>
#include "error.h"

int output_level = -1; 
FILE *debug_file, *info_file, *err_file, *null_file;


int set_output_level(int level)
{
	int oldlevel = output_level;
	output_level = level;
	if (!null_file) {
		null_file = fopen("/dev/null", "w");
		if (!null_file) {
			printf("Error opening /dev/null: %m\n");
			printf("Enabling all output\n");
			output_level = 3;
		}
	}
	if (output_level > 3)
		output_level = 3;
	switch (output_level) {
		case 0:
			err_file = null_file;
			info_file = null_file;
			debug_file = null_file;
			break;
		case 1:
			err_file = stderr;
			info_file = null_file;
			debug_file = null_file;
			break;
		case 2:
			err_file = stderr;
			info_file = stderr;
			debug_file = null_file;
			break;
		case 3:
			err_file = stderr;
			info_file = stderr;
			debug_file = stderr;
			break;
	}
	return oldlevel;
}

void do_output(FILE *outputfd, const char* file, int line, const char* fmt, ...)
{
	va_list val; 
	va_start(val, fmt); 
	fprintf(outputfd, "%s:%d ", basename(file), line); 
	vfprintf(outputfd, fmt, val); 
	va_end(val);
}

